package cn.sdhuijin.healthexam.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import cn.sdhuijin.healthexam.ExitApplication;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.bean.User;
import cn.sdhuijin.healthexam.ui.popup.MenuItem;
import cn.sdhuijin.healthexam.ui.popup.PopupMenu;
import cn.sdhuijin.healthexam.ui.popup.PopupMenu.OnItemSelectedListener;

/**
 * 首页
 * 
 * 修改完成
 * 
 * @author sunping
 */
public class IndexActivity extends AbstractActivity implements OnClickListener,
		OnItemSelectedListener {

	private static final int ID_NEW_CHILD = 1;
	private static final int ID_CHANQIAN_FIRST = 2;
	private static final int ID_DIABETES = 3;
	private static final int ID_HYPERTENSION = 4;
	private static final int ID_PSYCHIATRIC = 5;
	private static final int ID_CHANQIAN_SECOND = 6;
	private static final int ID_CHANHOU = 7;
	private static final int ID_CHANHOU_SISHIER = 8;
	private static final int ID_NEW_CHILD_MANYUE = 9;
	private static final int ID_NEW_CHILD_SHIERYUE = 10;
	private static final int ID_NEW_CHILD_SANSUI = 11;
	private static final int ID_NEW_CHILD_SANYUE = 12;
	private static final int ID_NEW_CHILD_LIUYUE = 13;
	private static final int ID_NEW_CHILD_BAYUE = 14;
	private static final int ID_NEW_CHILD_SHIBAYUE = 15;
	private static final int ID_NEW_CHILD_ERSHISIYUE = 16;
	private static final int ID_NEW_CHILD_SANSHIYUE = 17;
	private static final int ID_NEW_CHILD_SISUI = 18;
	private static final int ID_NEW_CHILD_WUSUI = 19;
	private static final int ID_NEW_CHILD_LIUSUI = 20;
	private static final int ID_CHANQIAN_THIRD = 21;
	private static final int ID_CHANQIAN_FORTH = 22;
	private static final int ID_CHANQIAN_FIFTH = 23;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_frame_index);
		ExitApplication.getInstance().addActivity(this);

		this.setButtonListener(this, R.id.bt_scan_identify, R.id.bt_archives,
				R.id.bt_exam, R.id.bt_visit, R.id.bt_export, R.id.bt_search,
				R.id.bt_exit, R.id.bt_update_pwd, R.id.bt_update,
				R.id.bt_physique);
	}

	@Override
	public void onClick(View v) {
		Intent intent = new Intent();
		int clickId = (Integer) v.getId();
		switch (clickId) {
		case R.id.bt_scan_identify:
			intent.putExtra("pageFrom", "扫描");
			intent.setClass(this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_archives:
			intent.putExtra("pageFrom", "档案");
			intent.setClass(this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_exam:
			intent.putExtra("pageFrom", "体检");
			intent.setClass(this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_physique:
			intent.putExtra("pageFrom", "中医体质判定");
			intent.setClass(this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_visit:
			showPopMenuItem(v);
			break;
		case R.id.bt_export:
			intent.setClass(this, ExportDataActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_search:
			intent.setClass(this, SearchInfoActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_exit:
			closeBluetoothConn();
			ExitApplication.getInstance().exit();
			break;
		case R.id.bt_update_pwd:
			intent.setClass(this, UpdatePWDActivity.class);
			startActivity(intent);
			break;
		case R.id.bt_update:
			intent.setClass(this, UpdateActivity.class);
			startActivity(intent);
			break;
		}

	}

	private void showPopMenuItem(View v) {
		PopupMenu menu = new PopupMenu(this);
		menu.setHeaderTitle("随访菜单");
		menu.setOnItemSelectedListener(this);
		menu.add(ID_CHANQIAN_FIRST, R.string.chanqian_first).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_CHANQIAN_SECOND, R.string.chanqian_second).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_CHANQIAN_THIRD, R.string.chanqian_third).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_CHANQIAN_FORTH, R.string.chanqian_forth).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_CHANQIAN_FIFTH, R.string.chanqian_fifth).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_CHANHOU, R.string.chanhou).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_CHANHOU_SISHIER, R.string.chanhou_sishier).setIcon(
				getResources().getDrawable(R.drawable.yunchanfu));
		menu.add(ID_NEW_CHILD, R.string.new_chiled).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_MANYUE, R.string.new_chiled_manyue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_SANYUE, R.string.new_chiled_sanyue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_LIUYUE, R.string.new_chiled_liuyue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_BAYUE, R.string.new_chiled_bayue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_SHIERYUE, R.string.new_chiled_shieryue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_SHIBAYUE, R.string.new_chiled_shibayue).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_ERSHISIYUE, R.string.new_chiled_ershisiyue)
				.setIcon(getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_SANSHIYUE, R.string.new_chiled_sanshiyue)
				.setIcon(getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_SANSUI, R.string.new_chiled_sansui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_SISUI, R.string.new_chiled_sisui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_WUSUI, R.string.new_chiled_wusui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_NEW_CHILD_LIUSUI, R.string.new_chiled_liusui).setIcon(
				getResources().getDrawable(R.drawable.xinshenger));
		menu.add(ID_DIABETES, R.string.diabetes).setIcon(
				getResources().getDrawable(R.drawable.tangniaobing));
		menu.add(ID_HYPERTENSION, R.string.hypertension).setIcon(
				getResources().getDrawable(R.drawable.gaoxueya));
		menu.add(ID_PSYCHIATRIC, R.string.psychiatric).setIcon(
				getResources().getDrawable(R.drawable.jingshenbing));
		menu.show(v);
	}

	/**
	 * 退出程序，关闭蓝牙连接
	 */
	private void closeBluetoothConn() {
		if (User.bluetoothConn) {
			User.conn.closeBlueToothConn();
			User.conn = null;
			User.bluetoothConn = false;
		}
	}

	@Override
	public void onItemSelected(MenuItem item) {
		Intent intent = new Intent();
		switch (item.getItemId()) {
		case ID_CHANQIAN_FIRST:
			intent.putExtra("pageFrom", "产前第一次随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_CHANQIAN_SECOND:
			intent.putExtra("pageFrom", "产前第二次随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_CHANQIAN_THIRD:
			intent.putExtra("pageFrom", "产前第三次随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_CHANQIAN_FORTH:
			intent.putExtra("pageFrom", "产前第四次随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_CHANQIAN_FIFTH:
			intent.putExtra("pageFrom", "产前第五次随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_CHANHOU:
			intent.putExtra("pageFrom", "产后随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_CHANHOU_SISHIER:
			intent.putExtra("pageFrom", "产后四十二天随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_HYPERTENSION:
			intent.putExtra("pageFrom", "高血压随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_DIABETES:
			intent.putExtra("pageFrom", "糖尿病随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_PSYCHIATRIC:
			intent.putExtra("pageFrom", "精神病随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD:
			intent.putExtra("pageFrom", "新生儿随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_MANYUE:
			intent.putExtra("pageFrom", "新生儿满月随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_SANYUE:
			intent.putExtra("pageFrom", "新生儿三月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_LIUYUE:
			intent.putExtra("pageFrom", "新生儿六月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_BAYUE:
			intent.putExtra("pageFrom", "新生儿八月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_SHIBAYUE:
			intent.putExtra("pageFrom", "新生儿十八月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_SHIERYUE:
			intent.putExtra("pageFrom", "新生儿十二月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_ERSHISIYUE:
			intent.putExtra("pageFrom", "新生儿二十四月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_SANSHIYUE:
			intent.putExtra("pageFrom", "新生儿三十月龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_SISUI:
			intent.putExtra("pageFrom", "新生儿四岁龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_WUSUI:
			intent.putExtra("pageFrom", "新生儿五岁龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_LIUSUI:
			intent.putExtra("pageFrom", "新生儿六岁龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		case ID_NEW_CHILD_SANSUI:
			intent.putExtra("pageFrom", "新生儿三岁龄随访");
			intent.setClass(IndexActivity.this, ScanIdentifyActivity.class);
			startActivity(intent);
			break;
		default:
			toastInformation(R.string.function_not_complete);
			break;
		}
	}

}
